package top.yesz.home.pojo;

import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;

@Getter
@Setter
public class AccessLog {
    //访问者用户名
    private String username;
    //请求路径
    private String url;
    //请求消耗时长
    private Integer duration;
    //http 方法：GET、POST等
    private String httpMethod;
    //http 请求响应状态码
    private Integer httpStatus;
    //访问者ip
    private String ip;
    //此条记录的创建时间
    private Date createTime;
    //请求参数
    private Map<String, String[]> parameterMap;

    @Override
    public String toString() {

        return "AccessLog{" +
                "username访问者用户名='" + username + '\'' +
                ", ip访问者ip='" + ip + '\'' +
                ", httpMethod请求方法='" + httpMethod + '\'' +
                ", url请求路径='" + url + '\'' +
                ", parameterMap请求参数=" + getParameterMaps() +
                ", createTime此条记录的创建时间=" + createTime +
                ", httpStatus请求响应状态码=" + httpStatus +
                ", duration请求消耗时长=" + duration +
                '}';
    }

    private String getParameterMaps() {
        StringBuilder builder = new StringBuilder();
        for (Map.Entry<String, String[]> stringEntry : parameterMap.entrySet()) {
            builder.append(stringEntry.getKey())
                    .append("=")
                    .append(Arrays.toString(stringEntry.getValue()));
        }
        if (StringUtils.isBlank(builder)) {
            builder.append("无参数");
        }
        return builder.toString();
    }
}
